package cc.ug_project.rollcall.Dialogs

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import cc.ug_project.rollcall.Bussness.DataModel.User
import cc.ug_project.rollcall.Bussness.DataProviders.UserDataProvider
import cc.ug_project.rollcall.R
import com.ug_project.views.click
import com.ug_project.views.content
import kotlinx.android.synthetic.main.dialog_edit_user.*

/**
 * Created by yeganaaa on 4/3/18.
 */
class EditUserDialog(context: Context?, val user: User, val provider: UserDataProvider, val action: (objectId: String, exception: Exception?) -> Unit) : Dialog(context) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_add_user)

        setCanceledOnTouchOutside(false)

        Delete.click {

        }

        Save.click {

            val name = Name.content
            val tell = TellNumber.content

             if(name.isEmpty() || tell.isEmpty()) {
                 ErrorMessage.text = context.getString(R.string.please_input_all_message)
                 return@click
             }

            val user = User().apply {
                this.name = name
                this.signInName = name
                this.tellNumber = tell
                this.weChatNumber = WeChatNumber.content
                this.qqNumber = QQNumber.content
            }

            provider.addUser(user) {objectId, exception ->
                action(objectId, exception)
                this.dismiss()
            }
        }
    }
}